package com.kly.user.serviceImpl.traffic.homepage;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.google.common.eventbus.AsyncEventBus;
import com.kly.service.MemberService;
import com.kly.user.db.dao.CustomersUserDao;
import com.kly.user.db.dao.IssueUserDao;
import com.kly.user.db.dao.UserDao;
import com.kly.user.db.entity.CustomersUserEntity;
import com.kly.user.db.entity.IssueUserPoolEntity;
import com.kly.user.db.entity.UserEntity;
import com.kly.user.db.entity.UserPreHomepageEntity;
import com.kly.user.enums.AppCodeEnum;
import com.kly.user.enums.AppEnum;
import com.kly.user.enums.GenderEnum;
import com.kly.user.eventbus.ActionEnum;
import com.kly.user.eventbus.Event;
import com.kly.user.service.UserTrafficSourceService;
import com.kly.user.service.WhiteListService;
import com.kly.user.task.UserTaskConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public abstract class HomepageBase {

    public static Log logger = LogFactory.getLog(HomepageBase.class);

    @Autowired
    UserDao userDao;

    @Resource
    WhiteListService whiteListService;

    @Resource
    MemberService memberService;


    @Resource
    AsyncEventBus pushEventBus;
    @Resource
    IssueUserDao issueUserDao;

    @Autowired
    CustomersUserDao customersUserDao;


    @Resource
    AsyncEventBus asyncEventBus;


    @Resource
    UserTaskConfig userTaskConfig;

    @Resource
    UserTrafficSourceService userTrafficSourceService;


    @NacosValue(value = "${meboo.homepage.new.policy.version:5}", autoRefreshed = true)
    public Integer homepageNewPolicyVersion;

    public List<UserEntity> queryUsers(UserEntity currentUser, Integer recordNum, Integer lookingFor) {
        return null;
    }

    public void updateConnectCount(List<UserEntity> userEntities, List<String> userCodeList) {
        if (!userCodeList.isEmpty()) {
            logger.info("==queryUsers 获取到的用户池不为空 ==,size:{}", userCodeList.size());
            Map<String, Object> selectUsersParams = new HashMap<>();
            selectUsersParams.put("list", userCodeList);
            List<UserEntity> userEntityList = userDao.selectUsers(selectUsersParams);
            if (!userEntityList.isEmpty()) {
                userEntities.addAll(userEntityList);
                issueUserDao.updateConnectCount(userCodeList);
            }
        }
    }

    public void sendPush(String userCode, List<UserEntity> userEntities, List<String> userCodeList) {
        if (CollectionUtils.isNotEmpty(userEntities)) {
            logger.info("==queryUsers 获取到的用户池不为空,缓存到首页使用的池子中去，方便过滤首页已经展示过的 ==,size:{}", userCodeList.size());
            final List<UserPreHomepageEntity> userPreHomepageEntities = userEntities.parallelStream().map(o -> {
                UserPreHomepageEntity entity = new UserPreHomepageEntity();
                entity.setUserCode(userCode);
                entity.setUserCodeWlm(o.getUserCode());
                entity.setEffectiveDate(DateUtil.offset(DateUtil.date(), DateField.SECOND, userTaskConfig.getCacheTime()));
                return entity;
            }).collect(Collectors.toList());

            try {
                final Event storeDataEvent = Event.builder().eventActionEnum(ActionEnum.STORE_PRE_HOME_DATA).toUserCode(userCode).app(AppEnum.MEBOO).content(JSONObject.toJSONString(userPreHomepageEntities)).build();
                asyncEventBus.post(storeDataEvent);
                logger.info("==queryUsers 获取到的用户池不为空,缓存到首页使用的池子中去，方便过滤首页已经展示过的 发送异步消息成功，等待异步保存完成 ==,size:{}", userCodeList.size());
            } catch (Exception e) {
                logger.error("==queryUsers 获取到的用户池不为空,缓存到首页使用的池子中去，发生异常 ==,size:{}", userCodeList.size(), e);
            }
        }
    }
}
